package com.jpz.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jpz.bean.Course;
import com.jpz.bean.Permission;
import com.jpz.service.PermissionService;
import com.jpz.utils.pageshow;

@Controller
@RequestMapping("/menu")
public class Menucontroller {
	@Autowired
	PermissionService permission;
	@RequestMapping("/show")
	public String show(Model model,Integer pageindex) {
		//分页查询
		Page<Permission> page = new Page<>(pageindex, 4);
		Page<Permission> page2 = permission.selectPage(page, new EntityWrapper<Permission>().eq("available", 1).eq("type", "menu"));
		List<Permission> lis = page2.getRecords();
		long total = page2.getTotal();
		int ii= new Long(total).intValue();  
		pageshow<Permission> pagebean = new pageshow<>(lis, ii,null , page2.getCurrent());
		model.addAttribute("pagebean", pagebean);
		return "/menus.jsp";
	}
	@ResponseBody
	@RequestMapping("/getonel")
	public List<Permission> getonel(){
		
		List<Permission> menus = permission.selectList(new EntityWrapper<Permission>().eq("type", "menu").eq("parentid", "0").eq("available", 1));
		for (Permission permission : menus) {
			System.out.println("获取的一级菜单"+permission);
		}
		return menus;
	}
	@ResponseBody
	@RequestMapping("/add")
	public boolean add(Permission p) {
		p.setAvailable("1");
		p.setType("menu");
		boolean b = permission.insert(p);
		return b;
	}
	@ResponseBody
	@RequestMapping("/getbyid")
	public Permission getbyid(Integer pid) {
		Permission per = permission.selectById(pid);
		return per;
	}

	@ResponseBody
	@RequestMapping("/updateOneLevelMenu")
	public boolean updateOneLevelMenu(Permission p) {
		return permission.updateById(p);
	}
	@ResponseBody
	@RequestMapping("/gexiner")
	public Map<String, Object> genxiner(Integer pid){
		
		Permission perm = permission.selectById(pid);
		List<Permission> menus = permission.selectList(new EntityWrapper<Permission>().eq("available", 1).eq("parentid", "0"));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("permission", perm);
		map.put("menus", menus);
		return map;
	}

}
